DECLARE @TOTAL_VENDA INT
DECLARE @TOTAL_TROCA INT
DECLARE @TOTAL_SAIDA INT
DECLARE @TOTAL_ENTRADA INT
DECLARE @TOTAL_GERAL VARCHAR(25)
DECLARE @DATA DATETIME
DECLARE @CODIGO_FILIAL VARCHAR(8)
DECLARE @FILIAL VARCHAR (25)
DECLARE @CODIGO_INVENTARIO CHAR (8)

SET @CODIGO_INVENTARIO = '11/03/08'


SET @CODIGO_FILIAL = (SELECT CODIGO_FILIAL FROM LOJAS_VAREJO WHERE FILIAL IN (SELECT FILIAL FROM INVENTARIO WHERE CODIGO_INVENTARIO = @CODIGO_INVENTARIO))
SET @FILIAL = (SELECT FILIAL FROM LOJAS_VAREJO WHERE CODIGO_FILIAL = @CODIGO_FILIAL)
SET @DATA = DATEADD(S, -1, (SELECT EMISSAO FROM INVENTARIO WHERE CODIGO_INVENTARIO = @CODIGO_INVENTARIO))


SELECT @TOTAL_VENDA = ISNULL((SELECT SUM(-QTDE) FROM LOJA_VENDA_PRODUTO
					  WHERE CODIGO_FILIAL = @CODIGO_FILIAL
				      AND DATA_VENDA <= '20080310 23:59'), 0)
PRINT 'LOJA_VENDA = ' + CONVERT(VARCHAR(25), @TOTAL_VENDA)
PRINT '----------------------------------------------------'

SELECT @TOTAL_TROCA = ISNULL((SELECT SUM(QTDE) FROM LOJA_VENDA_TROCA
					  WHERE CODIGO_FILIAL = @CODIGO_FILIAL
					  AND DATA_VENDA <= '20080310 23:59'), 0)
PRINT 'LOJA_VENDA_TROCA = ' + CONVERT(VARCHAR(25), @TOTAL_TROCA)
PRINT '----------------------------------------------------'

SELECT @TOTAL_ENTRADA = ISNULL((SELECT SUM((EN1 + EN2 + EN3 + EN4 + EN5 + EN6 + EN7 + EN8 + EN9 + EN10 + EN11 + EN12 + EN13 + EN14 + EN15 + EN16 +     
											EN17 + EN18 + EN19 + EN20 + EN21 + EN22 + EN23 + EN24 + EN25 + EN26 + EN27 + EN28 + EN29 + EN30 + EN31 + 
											EN32 + EN33 + EN34 + EN35 + EN36 + EN37 + EN38 + EN39 + EN40 + EN41 + EN42 + EN43 + EN44 + EN45 + EN46 + 
											EN47 + EN48)) FROM LOJA_ENTRADAS_PRODUTO A
								INNER JOIN LOJA_ENTRADAS B
									  ON A.FILIAL = B.FILIAL AND A.ROMANEIO_PRODUTO = B.ROMANEIO_PRODUTO
							    WHERE B.FILIAL = @FILIAL
						        AND B.EMISSAO <= '20080310 23:59'), 0)
PRINT 'LOJA_ENTRADAS = ' + CONVERT(VARCHAR(25), @TOTAL_ENTRADA)
PRINT '----------------------------------------------------'

SELECT @TOTAL_SAIDA = ISNULL((SELECT SUM(-(EN1 + EN2 + EN3 + EN4 + EN5 + EN6 + EN7 + EN8 + EN9 + EN10 + EN11 + EN12 + EN13 + EN14 + EN15 + EN16 +     
										   EN17 + EN18 + EN19 + EN20 + EN21 + EN22 + EN23 + EN24 + EN25 + EN26 + EN27 + EN28 + EN29 + EN30 + EN31 + 
										   EN32 + EN33 + EN34 + EN35 + EN36 + EN37 + EN38 + EN39 + EN40 + EN41 + EN42 + EN43 + EN44 + EN45 + EN46 + 
										   EN47 + EN48)) FROM LOJA_SAIDAS_PRODUTO A
							  INNER JOIN LOJA_SAIDAS B
									ON A.FILIAL = B.FILIAL AND A.ROMANEIO_PRODUTO = B.ROMANEIO_PRODUTO
					          WHERE B.FILIAL = @FILIAL
					          AND B.EMISSAO <= '20080310 23:59'), 0)
PRINT 'LOJA_SAIDAS = ' + CONVERT(VARCHAR(25), @TOTAL_SAIDA)
PRINT '----------------------------------------------------'

PRINT ''
SET @TOTAL_GERAL = (@TOTAL_VENDA + @TOTAL_TROCA + @TOTAL_ENTRADA + @TOTAL_SAIDA)
PRINT 'TOTAL MOVIMENTACOES = ' + ISNULL(@TOTAL_GERAL, 0)